<?php
/**
 * User: WL
 * Time: 13-11-20 - 上午9:14
 * File: buy.inc.php
 * Project For ixdcw.com
 */

defined('IN_IXDCW') or exit('Access Denied');
require DT_CORE.'/'.$module.'/common.inc.php';
require_once DT_INC.'/post.func.php';
login();
ViewCheck::catExists() or Prompt::listNotFound();
ViewCheck::groupListPermission() or Prompt::noPermission();
$do = new OwnCardModule();
$owncardAction = Db('owncard');
$PAY = PCache::read('pay.php');
switch($action) {
	case 'pay': //核对订购信息
		if(!$do->checkStatus($itemid,$_username,1))dheader(SITE_URL."owncard/buy.php");
		$buyinfo = $owncardAction->getByitem($itemid);
		extract($buyinfo);
		$note = str::stripSlashes($note);
		$total = $MOD['price'] * intval($buynum);
		$userinfo = userinfo($_username);
		if($userinfo['money'] < $total) {
			$payable = $total-$userinfo['money'];
		}
		if($submit){
			if($payable){
				$fee = $PAY[$bank]['percent'] ? dround($payable*$PAY[$bank]['percent']/100) : 0;
				$charge = dround($fee + $payable,2);
				$orderid = $itemid;
				$buyinfo['buybank']    = $bank;
				$buyinfo['money']   = $charge;
				$buyinfo['xdmoney'] = $userinfo['money'];
				$owncardAction->updateBuycard($buyinfo);
				$receive_url = SITE_URL."owncard/buy.php?action=success&itemid={$itemid}";
				$notify_url  = SITE_URL."owncard/".$bank."_notify.php";

				include DT_ROOT.'/api/pay/'.$bank.'/send.inc.php';
				exit;
			}else{
				//讯都账户扣款
				money_add($_username, -$total);
				money_record($_username, -$total, '站内', 'system', '车主金卡购买扣款',"订单号：$itemid");
				//更新订购信息
				$buyinfo['buytime'] = $DT_TIME;
				$buyinfo['xdmoney']   = $total;
				if($owncardAction->updateBuycard($buyinfo)) dheader("?action=success&itemid=$itemid&way=1");
			}
		}
		break;
	case 'edit': //修改订购信息
		if(!$do->checkStatus($itemid,$_username,1))dheader(SITE_URL."owncard/buy.php");
		$buyinfo = $owncardAction->getByitem($itemid);
		extract($buyinfo);
		$note = str::stripSlashes($note);
		if($status !=1 || !isset($status))dheader(SITE_URL."owncard/buy.php");
		if($submit){
			if($do->passBuy($post)){
				$post['itemid'] = $itemid;
				if($owncardAction->updateBuycard($post)) dheader("?action=pay&itemid=$itemid ");
			}else{
				Prompt::message($do->getErrmsg());
			}
		}
		break;
	case 'success': //成功付款订购
		$itemid or dheader(SITE_URL."owncard/buy.php");
		$buyinfo = $owncardAction->getByitem($itemid);
		extract($buyinfo);
		if($status == 2 || $status == 3){
			$message = '您已成功订购，我们的客服将尽快与您联系发货！';
		}elseif($status == 4){
			$fail = 1;
			$message ="很抱歉，订购失败！支付异常,请联系在线客服";
		}
		else{
			if($way && $money){
				if(!$do->checkStatus($itemid,$_username,1))dheader(SITE_URL."owncard/buy.php");
				$buyinfo['status']  = 2;
				$owncardAction->updateBuycard($buyinfo);
				$message = '您已成功订购，我们的客服将尽快与您联系发货！';
				send_message($username,"您的车主金卡订单已确认收到！","您已成功订购车主金卡($buynum)张，我们的客服将尽快与您联系发货！");
			}else{
				$charge_orderid = $itemid;
				$charge_money  = $money;
				$charge_amount = $money;
				if($buyinfo['status'] == 1) {
					$receive_url = '';
					$bank = $buyinfo['buybank'];
					$editor = 'R'.$bank;
					$note = '';
					include DT_ROOT.'/api/pay/'.$bank.'/receive.inc.php';
					if($charge_status == 1) {
						$buyinfo['status']  = 2;
						$buyinfo['buytime'] = $DT_TIME;
						if($owncardAction->updateBuycard($buyinfo))$buyinfo = $owncardAction->getByitem($itemid);
						extract($buyinfo);
						if($xdmoney){
							money_add($username, -$xdmoney);
							money_record($username, -$xdmoney, $PAY[$bank]['name'], 'system', '车主金卡购买', "订单号：$itemid");
						}
						$message = '您已成功订购，我们的客服将尽快与您联系发货！';
						send_message($username,"您的车主金卡订单已确认收到！","您已成功订购车主金卡($buynum)张，我们的客服将尽快与您联系发货！");
					} else {
						$fail = 1;
						$buyinfo['status']  = 4;
						$owncardAction->updateBuycard($buyinfo);
						$message ="很抱歉，订购失败！支付异常,请联系在线客服";
					}
				}
			}
		}
		//推荐商家
		$sqlNewBusiness = "
		select * from " . Table::name(Table::NAME_BUSINESS) . "
		where
		mainstore=1 and
		(status=3 or status =2)
	order by itemid DESC limit 10";
		$indexData = $db->getAll($sqlNewBusiness);
		break;
	default: //填写订购信息
		if($submit){
			if($do->passBuy($post)){
				$post['username'] = $_username;
				$post['buytime']  = $DT_TIME;
				$post['status']   = 1;
				$post['note'] = Str::addSlashes($post['note']);
				$itemid = $owncardAction->addBuycard($post);
				if($itemid){
					dheader("?action=pay&itemid=$itemid");
				}else{
					Prompt::message('提交失败');
				}
			}else{
				Prompt::message($do->getErrmsg());
			}
		}

}

$seo_file = 'list';
include DT_INC.'/seo.inc.php';
include template('buy', $module);

